package com.tadpole.music.view.activity.web;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.tadpole.music.R;
import com.tadpole.music.config.Constant;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.widget.LollipopFixedWebView;


/**
 * 蝌蚪代理 h5
 * Created By 孙岩
 * on 2020/8/7.
 */
public class H5DaiLiActivity extends BaseActivity {
    private View viewBack;
    private LollipopFixedWebView webView;
    private WebSettings webSettings;
    //标题
    private TextView tvTitle;
    private FrameLayout flLeft;
    private FrameLayout flRight;
    private ImageView ivImage;

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initViews();
        initListeners();
        setWebView();
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                AlertDialog.Builder b = new AlertDialog.Builder(H5DaiLiActivity.this);
                b.setTitle("Alert");
                b.setMessage(message);
                b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.confirm();
                    }
                });
                b.setCancelable(false);
                b.create().show();
                return true;
            }

        });
    }

    //初始化控件
    @SuppressLint("JavascriptInterface")
    private void initViews() {
        viewBack = findViewById(R.id.viewBack);
        webView = findViewById(R.id.webView);
        tvTitle = findViewById(R.id.tvTitle);
        ivImage = findViewById(R.id.ivImage);
        flLeft = findViewById(R.id.flLeft);
        flRight = findViewById(R.id.flRight);
    }

    //设置webview
    private void setWebView() {
        webSettings = webView.getSettings();
        //启用WebView内置缩放功能
        webSettings.setBuiltInZoomControls(true);
        //让WebView支持缩放
        webSettings.setSupportZoom(true);
        //设置WebView使用内置缩放机制时，是否展现在屏幕缩放控件上
        webSettings.setDisplayZoomControls(true);
        //让WebView支持DOM storage API
        webSettings.setDomStorageEnabled(true);
        //允许使用js
        webSettings.setJavaScriptEnabled(true);


        //设置WebView缓存模式 默认断网情况下不缓存
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);


        //让WebView支持可任意比例缩放
        webSettings.setUseWideViewPort(true);

        //让WebView支持播放插件
        webSettings.setPluginState(WebSettings.PluginState.ON);


        //设置在WebView内部是否允许访问文件
        webSettings.setAllowFileAccess(true);
        //处理跨域问题
        webSettings.setAllowUniversalAccessFromFileURLs(true);

        //设置脚本是否允许自动打开弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        // 加快HTML网页加载完成速度
        if (Build.VERSION.SDK_INT >= 19) {
            webSettings.setLoadsImagesAutomatically(true);
        } else {
            webSettings.setLoadsImagesAutomatically(false);
        }

        // 开启Application H5 Caches 功能
        webSettings.setAppCacheEnabled(true);
        // 设置编码格式
        webSettings.setDefaultTextEncodingName("utf-8");
        webView.addJavascriptInterface(new H5DaiLi(this), "android");

        String title = getIntent().getStringExtra("title");
        tvTitle.setText(title);
        String token = SpUtil.getInstance(this).getString(Constant.TOKEN, "");
        webView.loadUrl("file:///android_asset/dist/index.html#/?token=" + token);

    }

    //初始化监听器
    private void initListeners() {
        viewBack.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                H5DaiLiActivity.this.finish();
            }
        });

        webView.setWebChromeClient(
                new WebChromeClient() {
                    public void onProgressChanged(WebView view, int progress) {// 载入进度改变而触发
                        if (progress == 100) {
                            ivImage.setVisibility(View.GONE);
                        } else {
                            ivImage.setVisibility(View.VISIBLE);
                        }

                        super.onProgressChanged(view, progress);
                    }
                });

        flLeft.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                webView.post(new Runnable() {
                    @Override
                    public void run() {
                        webView.loadUrl("javascript:callJsFunctionLeft()");
                    }
                });
            }
        });

        flRight.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                webView.post(new Runnable() {
                    @Override
                    public void run() {
                        webView.loadUrl("javascript:callJsFunctionRight()");
                    }
                });
            }
        });
    }
}
